package com.zjj.lbw.ai;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.zhipu.ZhipuAiStreamingChatModel;

import java.util.concurrent.TimeUnit;

public class StreamingModelTest {
    public static void main(String[] args) {
        // 使用 openai的也可以，但是默认的demo这个apikey 不支持打字机流式调用
        ZhipuAiStreamingChatModel model = ZhipuAiStreamingChatModel.builder()
                .apiKey("9a6dfc4258104e28ad0c72db2ddda3e4.f6VL6AWRQxORuJpi")
                .build();

        model.generate("你好，你是谁？", new StreamingResponseHandler<AiMessage>() {
            @Override
            public void onNext(String token) {

                System.out.print(token);

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }

            @Override
            public void onError(Throwable error) {
                System.out.println(error);
            }
        });
    }
}
